﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpeechToText.STT
{
    public delegate void OnGetSpeech(DecodedSpeech Speech);

    public class VoiceDecoder: IDisposable
    {

        SoundDetector Detector;
        Control Owner;

        public double NoiseVolume { get; set; }
        public int SilentTime { get; set; }
        public string Languege { get; set; }
        public bool isWorked { get { return Detector.isWorked; } }

        public event OnGetSpeech OnGetSpeech;

        public VoiceDecoder(double NoiseVolume, int SilentTime, Control Owner, string Lang)
        {
            this.Owner = Owner;
            this.SilentTime = SilentTime;
            this.NoiseVolume = NoiseVolume;
            Languege = Lang;

            Detector = new SoundDetector(Owner);
            Detector.AsyncRecordDone += OnRecordDone;
        }

        void OnRecordDone(WavResource Wav)
        {
            DecodedSpeech D = new DecodedSpeech(Wav, Languege);
            if (OnGetSpeech != null) Owner.Invoke(OnGetSpeech, new object[] { D });
        }

        public void Start()
        {
            Detector.Start(NoiseVolume, SilentTime);
        }

        public void Stop()
        {
            Detector.Stop();
        }

        public void Dispose()
        {
            Detector.Dispose();
        }
    }
}
